<?php

/**
 * uniBoard Forums List
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_forums_list{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Parent Forum
	 *
	 * @var integer
	 */
	
	private $parent_forum	= 0;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 * @param integer $parent_forum
	 */
	
	function __construct( $appCore, $parent_forum = 0){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Set Parent
		$this -> parent_forum = $parent_forum;
		
		// Load Index Page
		$appCore -> loadLanguageFile('forums_and_topics');
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws forums list
	 *
	 */
	
	function draw(){
		
		// Any forums in ACL?
		if (empty($this -> appCore -> user_acl['forum_show']))
		{
			// return nothing
			return '';
		}
		else
		{
			// Forums Array
			$forums = array();
			
			// Forums Children Map
			$forums_map = array();
			
			// Load forums from MySQL
			$this -> appCore -> db -> query( 'SELECT f.forum_id, f.forum_parent, f.forum_type, f.forum_name, f.forum_description, f.forum_threads, f.forum_posts, f.forum_redirects
			FROM ' . DB_PREFIX . 'forums f
			WHERE f.forum_id IN(' . join(',', $this -> appCore -> user_acl['forum_show']) . ')
			ORDER BY f.forum_pos');
			
			// Put them in array
			while ( $forum_result = $this -> appCore -> db -> fetch_assoc())
			{
				// Put forum to map
				$forums_map[$forum_result['forum_parent']][] = $forum_result['forum_id'];
				
				// Add forum to list
				$forums[$forum_result['forum_id']] = $forum_result;
			}
			
			// Free result
			$this -> appCore -> db -> freeResult();
			
			// Load templates?
			if ( isset($forums_map[0]))
			{
				// Start templates
				$templates = new tpl_forums_list( $this -> appCore);
				
				// Build iteration map?
				$iteration_map = (array) class_cache::loadCache('forums_road_map');
				
				// Not found?
				if ( class_cache::$load_error)
				{
					// Iteration map is easy to build
					$iteration_map = array_reverse( array_keys( $this -> appCore -> getForums()));
					
					// Cache it
					class_cache::saveCache( 'forums_road_map', $iteration_map);
				}
				
				// Map Lenght?
				$map_lenght = count( $iteration_map);
				
				// Reiterate
				for ( $i = 0; $i < $map_lenght; $i ++)
				{
					// Forum exsists?
					if ( isset($forums[$iteration_map[$i]]) && isset($forums[$forums[$iteration_map[$i]]['forum_parent']]))
					{
						// Increase parents stat counters
						$forums[$forums[$iteration_map[$i]]['forum_parent']]['forum_threads'] += $forums[$iteration_map[$i]]['forum_threads'];
						$forums[$forums[$iteration_map[$i]]['forum_parent']]['forum_posts'] += $forums[$iteration_map[$i]]['forum_posts'];
					}
				}
				
				// Garbage out
				unset( $map_lenght);
				unset( $iteration_map);
				
				// Drawed categories
				$drawed_cats = '';
				
				// Descriptions Parser
				$parser = new class_parser($this -> appCore);
				
				// Number of forums in main category
				$main_cats = count($forums_map[0]);
				
				// Reiterate forums
				for ( $i = 0; $i < $main_cats; $i ++)
				{
					// Forum has children?
					if (isset($forums_map[$forums_map[0][$i]]))
					{
						// Category content
						$category_content = '';
						
						// Number of forums
						$forums_n = count($forums_map[$forums_map[0][$i]]);
						
						// Reiterate Forums
						for ( $f = 0; $f < $forums_n; $f ++)
						{
							// Draw Forum
							$category_content .= $templates -> tpl_forum_body(array(
								'forum_id'				=> $forums_map[$forums_map[0][$i]][$f],
								'forum_name'			=> htmlspecialchars( $forums[$forums_map[$forums_map[0][$i]][$f]]['forum_name']),
								'forum_description'		=> $parser -> finalise( $forums[$forums_map[$forums_map[0][$i]][$f]]['forum_description'])
							));
						}
						
						// Draw category body
						$drawed_cats .= $templates -> tpl_category_body(array(
							'forum_id'				=> $forums_map[0][$i],
							'forum_name'			=> htmlspecialchars( $forums[$forums_map[0][$i]]['forum_name']),
							'forum_description'		=> $parser -> finalise( $forums[$forums_map[0][$i]]['forum_description']),
							'subforums' 			=> $category_content
						));
					}
				}
				
				// Return drawed cats
				return $drawed_cats;
			}
			else
			{
				// Return empty strin
				return '';
			}
		}
		
	}
	
}